using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace TripTracker
{
    class GPSDataParser
    {

        private Boolean lat = true;
        private Boolean lon = false;
        private Regex regex1 = new Regex("[0*]");
        private Regex regex2 = new Regex("[0*][.][0*]");

        /// <summary>
        /// Checks if coordinate is not empty, returns the coordinates converted in Google-Earth format
        /// </summary>
        /// <param name="gpsData">GPS raw data</param>
        /// <returns>latitude|longitude|altitude|timestamp</returns>
        public String GetCoordinates(String gpsData) {
            
            String result = "";
            String[] lines = gpsData.Split('\n');

            for (int i = 0; i < lines.Length-1; i++)
			{
		        String[] data = lines[i].Split(',');
                if (data[0].Equals("$GPGGA") && !data[2].Equals("") &&
                    !regex1.IsMatch(data[2]) && !regex2.IsMatch(data[2]))
                {
                    result += convertCoordinate(data[2], data[3], lat) + "|" +
                                convertCoordinate(data[4], data[5], lon) + "|" +
                                data[9] + "|" + data[1];
                    //latitude|longitude|altitude|timestamp
                    break;
                }
			}

            return result;
        }

        /// <summary>
        /// Converts a NMEA coordinate into a decimal coordinate
        /// The North-Coordinate is positive when the point is in the nothern hemisphere,
        /// negative when the pointis in the southern hemisphere
        /// The East-Coordinate is positive when the point is on the east of the Prime Meridian,
        /// negaative when it is on the west of the Prime Meridian
        /// </summary>
        /// <param name="NMEACoord">the NMEA coordinate</param>
        /// <param name="type">the type od the coordinate, either 1 for latitude or 0 for longitude</param>
        /// <returns>the decimal coordinate corresponding to the NMEA coordinate</returns>
        public String convertCoordinate(String NMEACoord, String hemi, Boolean type)
        {

            int set;
            if (type == lat)
            {
                //coordinate type is latitude
                set = 2;
            }
            else
            {
                //coordinate type is longitude
                set = 3;
            }

            try
            {

                String decString = NMEACoord.Substring(0, set);
                String minString = NMEACoord.Substring(set, NMEACoord.Length - set);
                int dec = 0;
                
                try
                {
                    dec = Convert.ToInt16(decString);
                }
                catch (Exception e)
                {
                    MessageBox.Show("Warning! " + e.Message);
                } 
                     
                minString = minString.Replace('.', ',');
                double min = Double.Parse(minString);
                double decimalcoord = (dec * 1.0) + (min / 60.0);
                if (hemi == "S" || hemi == "W")
                {
                    decimalcoord = -decimalcoord;
                }
                String result = decimalcoord.ToString();
                result = result.Replace(',', '.');
                return result;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            return "";
        }
    }
}
